package com.kuaimacode.kframework.agent.services.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.component.ComponentUtil;
import com.kuaimacode.kframework.exception.ErrorEnum;
import com.kuaimacode.kframework.exception.ServerInternalErrorException;
import com.kuaimacode.kframework.mybatis.mapper.user.UserMapper;
import com.kuaimacode.kframework.mybatis.models.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * @author Administrator
 * @date 2019/03/18
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    @Autowired
    private ComponentUtil componentUtil;

    /**
     * 分页查找所有的数据
     *
     * @param pageNum  当前页码，从1开始
     * @param pageSize 一页显示多少行
     * @param searchUser
     * @return
     */
    public PageInfo<User> findAll(int pageNum, int pageSize, User searchUser) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        Optional.ofNullable(searchUser.getPhone())
                .filter(phone -> !"".equals(phone.trim())).ifPresent(phone -> queryWrapper.like("phone", phone));
        Optional.ofNullable(searchUser.getEnabled())
                .ifPresent(enable -> queryWrapper.eq("enabled", enable));
        Optional.ofNullable(searchUser.getIsMaster())
                .ifPresent(master -> queryWrapper.eq("is_master", master));
        Optional.ofNullable(searchUser.getIsCertifacate())
                .ifPresent(certificate -> queryWrapper.eq("is_certifacate", certificate));
        List<User> datas = baseMapper.selectList(queryWrapper);
        //处理图片地址
        Optional.ofNullable(datas)
                .ifPresent(users -> users.forEach(user ->
                        Optional.ofNullable(user.getAvatarUrl())
                        .ifPresent(url -> user.setAvatarUrl(componentUtil.convertImgUrl(url)))
                ));
        return new PageInfo<>(datas);
    }

    public User selectByName(String username) {

        QueryWrapper<User> qryWrapper = new QueryWrapper<>();
        qryWrapper.eq("username", username);

        return baseMapper.selectOne(qryWrapper);
    }

    public User selectByPhone(String phone) {
        QueryWrapper<User> qryWrapper = new QueryWrapper<>();
        qryWrapper.eq("phone", phone);
        return baseMapper.selectOne(qryWrapper);
    }

    /**
     * 查询合作商家列表
     * @param pageNum
     * @param pageSize
     * @param searchUser
     * @return
     */
    public PageInfo<User> findPartnerPage(int pageNum, int pageSize, User searchUser) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        Optional.ofNullable(searchUser.getPartnerName())
                .filter(partnerName -> !"".equals(partnerName.trim())).ifPresent(partnerName -> queryWrapper.like("partner_name", partnerName));
        queryWrapper.isNotNull("is_partner");
        queryWrapper.eq("partner_agent", SessionUtil.getAgentId());
        List<User> datas = baseMapper.selectList(queryWrapper);
        return new PageInfo<>(datas);
    }

    /**
     * 修改商家信息
     * @param user
     * @return
     */
    public boolean editPartner(User user) {
        Optional.ofNullable(user.getId()).orElseThrow(() -> new ServerInternalErrorException(ErrorEnum.PARAM_VALID_ERROR));
        User partner = new User();
        Optional.ofNullable(user.getPartnerProtocol()).ifPresent(partner::setPartnerProtocol);
        Optional.ofNullable(user.getIsPartner()).ifPresent(partner::setIsPartner);
        Optional.ofNullable(user.getPartnerRemark()).ifPresent(partner::setPartnerRemark);
        Optional.ofNullable(user.getPartnerAmount()).ifPresent(partner::setPartnerAmount);
        Optional.ofNullable(user.getPartnerScale()).ifPresent(partner::setPartnerScale);
        partner.setId(user.getId());
        return baseMapper.updateById(partner) == 1;
    }

    public void agreePartner(Long id, BigDecimal scale) {
        User user = baseMapper.selectById(id);
        user.agreePartner(scale);
        baseMapper.updateById(user);
    }

    public void rejectPartner(Long id, String reason) {
        User user = baseMapper.selectById(id);
        user.rejectPartner(reason);
        baseMapper.updateById(user);
    }
}

